import multiprocessing

import requests
import ConnectMySql
import numpy
import re
from urllib import parse

conn, cursor = ConnectMySql.openConn()


def formatStr(str):
    re_compile = re.compile(u'[\U00010000-\U0010ffff]')
    formatStr = re_compile.sub(u'??', str)
    return formatStr


def getMusicComment(musicRid):
    url = "http://comment.kuwo.cn/com.s?type=get_rec_comment&uid=0&prod=newWeb&digest=15&sid=%s&page=1&rows=50&f=web&gid=d1ea185e-55bc-406e-b9e9-d03d136b151f" % str(
        musicRid)
    response = requests.get(url)
    result = response.json()
    try:
        # cursor.execute("select mid from comment")
        # mIdArray = cursor.fetchall()
        # mIdArray = numpy.array(mIdArray)
        # if musicRid not in mIdArray:
        if "rows" in result:
            data = result["rows"]
            for d in data:
                # parse.unquote 解决类似这种url编码格式的字符串 （%EF%B8%B7）
                print(formatStr(d["msg"]))
                if "reply" not in d.keys():
                    cursor.execute("insert into comment VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
                                   (int(d["id"]), d["u_pic"], formatStr(parse.unquote(d["u_name"])),
                                    d["time"], int(d["like_num"]),
                                    formatStr(d["msg"]), int(d["u_id"]), musicRid, 0,
                                    int(d["state"]), 0, 0))
                    conn.commit()
                else:
                    cursor.execute("insert into comment VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
                                   (int(d["id"]), d["u_pic"], formatStr(parse.unquote(d["u_name"])),
                                    d["time"],
                                    int(d["like_num"]), formatStr(d["msg"]), int(d["u_id"]), musicRid,
                                    0,
                                    int(d["state"]), 0, 1))
                    conn.commit()

                    cursor.execute("insert into relation VALUES (0,%s,%s,%s)",
                                   (int(d["id"]), int(d["reply"]["id"]), musicRid))
                    conn.commit()
    except Exception as e:
        print(e)
        pass


if __name__ == '__main__':
    pool = multiprocessing.Pool()

    cursor.execute("select musicRid from music where artistId = 680")
    musicArray = cursor.fetchall()
    musicArray = numpy.array(musicArray)

    for music in musicArray:
        musicRid = music[0]
        pool.apply_async(getMusicComment, args=(musicRid,))

    # for i in range(len(musicArray)):
    #     musicRid = musicArray[i][0]
    #     getMusicComment(musicRid)

    pool.close()
    pool.join()

    ConnectMySql.closeConn(conn, cursor)
